#include "slepui.h"
#include "cslep.h"
#include "slepbase.h"
#include <QtGui>


void SLEPUI::actLeastR(){
	optsWidget = new OptionsWidget(Method_Least_R);
	LogWidget::instance()->log(QString("Executing Least R"));
	int success = optsWidget->exec();
	if(success == QDialog::Accepted){
		OPTS *opts = optsWidget->getOPTS();
		slep_matrix* A = optsWidget->getInputA();
		slep_vector* y = optsWidget->getInputy();
		slep_vector* x = slepAllocDenseVector(A->n);
		slep_vector* funVal = slepAllocDenseVector(opts->maxIter);
		slep_vector* valueL = slepAllocDenseVector(opts->maxIter);

		LeastR(A,y,optsWidget->getL1RegCoefficient(),opts,x,funVal,valueL);
		LogWidget::instance()->log(QString("Success executing Least R"));

		SLEPItem *itemA = FileOperations::slepMatrix2SlepItem(A);
		SLEPItem *itemy = FileOperations::slepVector2SlepItem(y);
		SLEPItem *itemx = FileOperations::slepVector2SlepItem(x);
		SLEPItem *itemFVal = FileOperations::slepVector2SlepItem(funVal);

  		writeToCurrentDir(itemA,tr("A"));
		writeToCurrentDir(itemy,tr("y"));
		writeToCurrentDir(itemx,tr("x"));
		writeToCurrentDir(itemFVal,tr("funVal"));

		slepFreeMatrix(A);
		slepFreeVector(x);
		slepFreeVector(y);
		slepFreeVector(funVal);
		slepFreeVector(valueL);
		QMessageBox::information(this,tr("Success"), tr("It ran successfully!"));
	}
	else{
		QMessageBox::information(this,tr("Error"), tr("Was not able to execute as there were errors in input!"));
	}
	delete optsWidget;
}




void SLEPUI::actnnLeastR(){
	optsWidget = new OptionsWidget(Method_nnLeast_R);
	int success = optsWidget->exec();
	if(success == QDialog::Accepted){
		OPTS *opts = optsWidget->getOPTS();
		slep_matrix* A = optsWidget->getInputA();
		slep_vector* y = optsWidget->getInputy();
		slep_vector* x = slepAllocDenseVector(A->n);
		slep_vector* funVal = slepAllocDenseVector(opts->maxIter);
		slep_vector* valueL = slepAllocDenseVector(opts->maxIter);

		nnLeastR(A,y,optsWidget->getL1RegCoefficient(),opts,x,funVal,valueL);

		SLEPItem *itemA = FileOperations::slepMatrix2SlepItem(A);
		SLEPItem *itemy = FileOperations::slepVector2SlepItem(y);
		SLEPItem *itemx = FileOperations::slepVector2SlepItem(x);
		SLEPItem *itemFVal = FileOperations::slepVector2SlepItem(funVal);

  		writeToCurrentDir(itemA,tr("A"));
		writeToCurrentDir(itemy,tr("y"));
		writeToCurrentDir(itemx,tr("x"));
		writeToCurrentDir(itemFVal,tr("funVal"));


		slepFreeMatrix(A);
		slepFreeVector(x);
		slepFreeVector(y);
		slepFreeVector(funVal);
		slepFreeVector(valueL);
		QMessageBox::information(this,tr("Success"), tr("It ran successfully!"));
	}
	else{
		QMessageBox::information(this,tr("Error"), tr("Was not able to execute as there were errors in input!"));
	}
	delete optsWidget;
}



void SLEPUI::actLeastC(){
	optsWidget = new OptionsWidget(Method_Least_C);
	int success = optsWidget->exec();
	if(success == QDialog::Accepted){
		OPTS *opts = optsWidget->getOPTS();
		slep_matrix* A = optsWidget->getInputA();
		slep_vector* y = optsWidget->getInputy();
		slep_vector* x = slepAllocDenseVector(A->n);
		slep_vector* funVal = slepAllocDenseVector(opts->maxIter);
		slep_vector* valueL = slepAllocDenseVector(opts->maxIter);

		LeastC(A,y,optsWidget->getL1RegCoefficient(),opts,x,funVal,valueL);

		SLEPItem *itemA = FileOperations::slepMatrix2SlepItem(A);
		SLEPItem *itemy = FileOperations::slepVector2SlepItem(y);
		SLEPItem *itemx = FileOperations::slepVector2SlepItem(x);
		SLEPItem *itemFVal = FileOperations::slepVector2SlepItem(funVal);

  		writeToCurrentDir(itemA,tr("A"));
		writeToCurrentDir(itemy,tr("y"));
		writeToCurrentDir(itemx,tr("x"));
		writeToCurrentDir(itemFVal,tr("funVal"));

  
		slepFreeMatrix(A);
		slepFreeVector(x);
		slepFreeVector(y);
		slepFreeVector(funVal);
		slepFreeVector(valueL);
		QMessageBox::information(this,tr("Success"), tr("It ran successfully!"));
	}
	else{
		QMessageBox::information(this,tr("Error"), tr("Was not able to execute as there were errors in input!"));
	}
	delete optsWidget;
}



void SLEPUI::actnnLeastC(){
	optsWidget = new OptionsWidget(Method_nnLeast_C);
	int success = optsWidget->exec();
	if(success == QDialog::Accepted){
		OPTS *opts = optsWidget->getOPTS();
		slep_matrix* A = optsWidget->getInputA();
		slep_vector* y = optsWidget->getInputy();
		slep_vector* x = slepAllocDenseVector(A->n);
		slep_vector* funVal = slepAllocDenseVector(opts->maxIter);
		slep_vector* valueL = slepAllocDenseVector(opts->maxIter);

		nnLeastC(A,y,optsWidget->getL1RegCoefficient(),opts,x,funVal);

		SLEPItem *itemA = FileOperations::slepMatrix2SlepItem(A);
		SLEPItem *itemy = FileOperations::slepVector2SlepItem(y);
		SLEPItem *itemx = FileOperations::slepVector2SlepItem(x);
		SLEPItem *itemFVal = FileOperations::slepVector2SlepItem(funVal);

  		writeToCurrentDir(itemA,tr("A"));
		writeToCurrentDir(itemy,tr("y"));
		writeToCurrentDir(itemx,tr("x"));
		writeToCurrentDir(itemFVal,tr("funVal"));


		slepFreeMatrix(A);
		slepFreeVector(x);
		slepFreeVector(y);
		slepFreeVector(funVal);
		slepFreeVector(valueL);
		QMessageBox::information(this,tr("Success"), tr("It ran successfully!"));
	}
	else{
		QMessageBox::information(this,tr("Error"), tr("Was not able to execute as there were errors in input!"));
	}
	delete optsWidget;
}